//
//  TYScenarioInterpretation.m
//  AngerDragon
//
//  Created by gaotime_macbook_2 on 11-12-14.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TYScenarioInterpretationSingleton.h"
#import "cocos2d.h"
#import "TYCreateSpriteProtocol.h"

@implementation TYScenarioInterpretationSingleton

static TYScenarioInterpretationSingleton *myInstance = nil;



/// ios实现单例, 要这样做, 因为没有私有构造的概念
+(id)alloc {
	NSAssert(myInstance == nil, @"Attempted to allocate a second instance of a singleton.");
	return [super alloc];
}

/// 单例方法
+ (id<TYScenarioInterpretationProtocol>) sharedInstance {
	if (nil == myInstance) {
		
		myInstance = [[[self class] alloc] init];
	}
	
	return myInstance;
}



- (id) init {
	
	if ((self = [super init])) {
		CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
		scenarioCurrentTime = 0.f;
		
	}
	
	return self;
}

- (void) dealloc {
	CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	
	/// 
	myInstance = nil;
	
	
	[super dealloc];
}

#pragma mark 实现 对外的接口行为
/// 随着时间轴来解读剧本, 然后创建相应精灵对象,挂接到对应的集合中
- (void) scenarioInterpretationLoop:(ccTime) delta {
    scenarioCurrentTime += delta;
    ///读取剧本，根据剧本精灵列表中精灵出场顺序取出该出场的精灵，直到判断出该精灵出场时间大于当前时间截止
    NSInteger listNum = 0;
    for(int i=0;i<listNum;i++){
        CGFloat inTime = 0;
        if (inTime>scenarioCurrentTime) {
            break;
        }else{
            ///取出精灵方阵
            NSInteger num = 0;
            for(int j=0;j<num;j++){
                ///取出精灵个体
                NSString* spriteName = nil;
                ///根据名字生成接口类类型的类实例
                id<TYCreateSpriteProtocol> myClass = (NSClassFromString(spriteName));
                if (myClass != nil) {
                    NSInteger lifeMax = 0;
                    NSInteger moveSpeed = 0;
                    NSString* weaponName = nil;
                    NSInteger attackRadius = 0;
                    CGFloat attackFrequency = 0.f;
                    NSInteger weaponHurtValue = 0;
                    CGFloat bulletMoveSpeed = 0.f;
                    [myClass setLifeMax:lifeMax];
                    [myClass setMoveSpeed:moveSpeed];
                    [myClass addWeaponForName:weaponName setAttackRadius:attackRadius setAttackFrequency:attackFrequency setWeaponHurtValue:weaponHurtValue setBulletMoveSpeed:bulletMoveSpeed];
                }
            }
        }
    }
    ///根据
}
/// 告诉外界, 本关游戏是否胜利
- (BOOL) isWin {
    BOOL isWin = NO;
    switch (levelType) {
        case LEVEL_TYPE_NORMAL:
            if (enemySpriteDeadNum>=enemySpriteCount) {
                isWin = YES;
            }
            break;
        case LEVEL_TYPE_SPECIAL:
            if (scenarioCurrentTime>=scenarioTotalTime) {
                isWin = YES;
            }
            
        default:
            break;
    }
	return isWin;
}
/// 敌人精灵在其被player消灭时, 调用此接口, 来告知剧本模块累加已经死亡的敌对精灵
- (void) oneEnemySpriteDead {
    enemySpriteDeadNum ++;
}

/// 加载目标关卡的剧本
- (void) loadDestLevelScenario:(NSInteger) a_n_dest_level {
    ///根据关卡ID去取该关卡的剧本文件、精灵属性文件、精灵队列信息，把内容加载到内存中
    
    ///获取设置敌人精灵总数
    enemySpriteCount = 0;
    levelType = 0;
    
}

 
@end
